package com.zmn.oms.admin.config;

import com.alibaba.fastjson.parser.ParserConfig;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.ToStringSerializer;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.google.common.collect.Lists;
import com.zmn.oms.admin.converter.CodeEnumConverterFactory;
import com.zmn.oms.admin.converter.YearMonthConverter;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.http.CacheControl;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import java.math.BigInteger;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * ViewResolver配置
 *
 * @uthor nowind
 * @date 2018/9/17 11:41
 */
@Configuration
public class SpringMvcConfig extends WebMvcConfigurationSupport {

    /**
     * 页面匹配模式
     * setUseSuffixPatternMatch: 设置是否是后缀模式匹配，如“/user”是否匹配/user.*，默认真即匹配，为true时，/user.html、/user.aa、/user.* 都能访问；
     * setUseTrailingSlashMatch: 设置是否自动后缀路径模式匹配，如“/user”是否匹配“/user/”，默认真即匹配，为true时，/user、/user/ 都能访问；
     *
     * @param configurer
     */
    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        configurer.setUseSuffixPatternMatch(true)
                .setUseTrailingSlashMatch(true);
    }

    /**
     * 静态资源映射
     * addResourceHandler指的是访问路径，addResourceLocations指的是文件放置的目录
     *
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**")
                .addResourceLocations("classpath:/static/", "classpath:/template/")
                .setCacheControl(CacheControl.maxAge(5, TimeUnit.SECONDS).cachePublic());
    }

    @Override
    protected void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        //创建fastJson消息转换器
        FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();

        ArrayList<MediaType> mediaTypes = Lists
                .newArrayList(
                        new MediaType("application", "json", Charset.forName("UTF-8")),
                        new MediaType("application", "*+json", Charset.forName("UTF-8"))
                );
        FastJsonConfig config = new FastJsonConfig();
        SerializeConfig serializeConfig = new SerializeConfig();
        serializeConfig.put(BigInteger.class, ToStringSerializer.instance);
        serializeConfig.put(Long.class, ToStringSerializer.instance);
        serializeConfig.put(Long.TYPE, ToStringSerializer.instance);
        config.setSerializeConfig(serializeConfig);

        // 序列化的时候关闭ASM
        ParserConfig.getGlobalInstance().setAsmEnable(false);
        SerializeConfig.getGlobalInstance().setAsmEnable(false);

        fastConverter.setSupportedMediaTypes(mediaTypes);
        fastConverter.setFastJsonConfig(config);
        //将fastjson添加到视图消息转换器列表内
        converters.add(fastConverter);
    }

    @Override
    protected void addFormatters(FormatterRegistry registry) {
        // 添加java.time.YearMonth类型转换支持
        registry.addConverter(new YearMonthConverter());
        // 添加CodeEnum枚举类型转换支持
        registry.addConverterFactory(new CodeEnumConverterFactory());
    }

}